package com.link4a.doms.dao.handler;

import com.link4a.doms.dao.Page;
import com.link4a.doms.dao.SqlSessionTemplateSupport;
import com.link4a.doms.model.User;
import com.link4a.doms.vo.UserCondition;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author brown
 */
@Repository
public class UserHandler extends SqlSessionTemplateSupport {
	
	public List<User> findAll() {
		return getSqlSession().selectList("findAll");
	}

	public User findByName(String name) {
		return getSqlSession().selectOne("findByName", name);
	}

	public Page<User> findPage(UserCondition condition) {
		return findPage(condition,"countPage","findPage");
	}

	public void delete(String name) {
		getSqlSession().delete("delete", name);
	}

	public void create(User user) {
		getSqlSession().insert("create", user);
	}

	public void update(User user) {
		getSqlSession().update("update", user);
	}

	public void updateLastVisitTime(String name) {
		getSqlSession().update("updateLastVisitTime", name);
	}

	public void updateApplyCode(String username, String applyCode) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("applyCode", applyCode);
		param.put("username", username);
		getSqlSession().update("updateApplyCode", param);
	}

	public void resetPassword(String applyCode, String password) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("applyCode", applyCode);
		param.put("password", password);
		getSqlSession().update("resetPassword", param);
	}
	
	/**
	 * 判断链接是否过期
	 */
	public boolean isExpire(Date applyDate) {
		return new Date().getTime() - applyDate.getTime() > DateUtils.MILLIS_PER_DAY;
	}
}
